/*
 * 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
 * 开发板官网：www.lckfb.com
 * 技术支持常驻论坛，任何技术问题欢迎随时交流学习
 * 立创论坛：https://oshwhub.com/forum
 * 关注bilibili账号：【立创开发板】，掌握我们的最新动态！
 * 不靠卖板赚钱，以培养中国工程师为己任
 * Change Logs:
 * Date           Author       Notes
 * 2024-03-26     LCKFB-LP    first version
 */
#include "board.h"

static uint32_t systick_us = 0;
//static uint32_t systick_ms = 0;

void board_init(void)
{
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	//选择外部时钟
	systick_us=SystemCoreClock/8000000;	      				//为系统时钟的1/8
//	systick_ms=(uint32_t)systick_us*1000;					//每毫秒SysTick的VAL减的数目  	
}

void delay_us(uint32_t us)
{
	uint32_t systickCount;	
	uint32_t count = us * systick_us;
	
	if(us == 1)
	{
		SysTick->LOAD=count - 8;
	}
	else
	{
		SysTick->LOAD=count -10; 	//时间加载
	}	
	
	SysTick->VAL=0x00;        	//清空计数器	
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始计数	  
	
	do
	{
		systickCount=SysTick->CTRL;
	}while((systickCount&0x01)&&!(systickCount&(1<<16))); //等待时间到达   
	SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
	SysTick->VAL =0X00;       //清空计数器		
	
}

void delay_ms(uint32_t ms){ delay_us(ms * 1000); }

void delay_1us(uint32_t us){ delay_us(us); }

void delay_1ms(uint32_t ms){ delay_us(ms * 1000); }

static GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {
	TAMPER_BUTTON_GPIO_PORT,
	// KEY_BUTTON_GPIO_PORT,
	// RIGHT_BUTTON_GPIO_PORT,
	// LEFT_BUTTON_GPIO_PORT,
	// UP_BUTTON_GPIO_PORT,
	// DOWN_BUTTON_GPIO_PORT,
	// SEL_BUTTON_GPIO_PORT
};

static const uint16_t BUTTON_PIN[BUTTONn] = {
	TAMPER_BUTTON_PIN,
	// KEY_BUTTON_PIN,
	// RIGHT_BUTTON_PIN,
	// LEFT_BUTTON_PIN,
	// UP_BUTTON_PIN,
	// DOWN_BUTTON_PIN,
	// SEL_BUTTON_PIN
};

static const uint32_t BUTTON_CLK[BUTTONn] = {
	TAMPER_BUTTON_GPIO_CLK,
	// KEY_BUTTON_GPIO_CLK,
	// RIGHT_BUTTON_GPIO_CLK,
	// LEFT_BUTTON_GPIO_CLK,
	// UP_BUTTON_GPIO_CLK,
	// DOWN_BUTTON_GPIO_CLK,
	// SEL_BUTTON_GPIO_CLK
};


static const uint32_t BUTTON_EXTI_LINE[BUTTONn] = {
	TAMPER_BUTTON_EXTI_LINE,
	// KEY_BUTTON_EXTI_LINE,
	// RIGHT_BUTTON_EXTI_LINE,
	// LEFT_BUTTON_EXTI_LINE,
	// UP_BUTTON_EXTI_LINE,
	// DOWN_BUTTON_EXTI_LINE,
	// SEL_BUTTON_EXTI_LINE
};

// const uint8_t BUTTON_PORT_SOURCE[BUTTONn] = {
// 	TAMPER_BUTTON_EXTI_PORT_SOURCE,
// 	// KEY_BUTTON_EXTI_PORT_SOURCE,
// 	// RIGHT_BUTTON_EXTI_PORT_SOURCE,
// 	// LEFT_BUTTON_EXTI_PORT_SOURCE,
// 	// UP_BUTTON_EXTI_PORT_SOURCE,
// 	// DOWN_BUTTON_EXTI_PORT_SOURCE,
// 	// SEL_BUTTON_EXTI_PORT_SOURCE
// };

// const uint8_t BUTTON_PIN_SOURCE[BUTTONn] = {
// 	TAMPER_BUTTON_EXTI_PIN_SOURCE,
// 	// KEY_BUTTON_EXTI_PIN_SOURCE,
// 	// RIGHT_BUTTON_EXTI_PIN_SOURCE,
// 	// LEFT_BUTTON_EXTI_PIN_SOURCE,
// 	// UP_BUTTON_EXTI_PIN_SOURCE,
// 	// DOWN_BUTTON_EXTI_PIN_SOURCE,
// 	// SEL_BUTTON_EXTI_PIN_SOURCE
// };

static const uint8_t BUTTON_IRQn[BUTTONn] = {
	TAMPER_BUTTON_EXTI_IRQn,
	// KEY_BUTTON_EXTI_IRQn,
	// RIGHT_BUTTON_EXTI_IRQn,
	// LEFT_BUTTON_EXTI_IRQn,
	// UP_BUTTON_EXTI_IRQn,
	// DOWN_BUTTON_EXTI_IRQn,
	// SEL_BUTTON_EXTI_IRQn
};

void stm32_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHBPeriphClockCmd(BUTTON_CLK[Button], ENABLE);
	// RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = BUTTON_PIN[Button];
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStructure);

	if (Button_Mode == BUTTON_MODE_EXTI)
	{
		// 配置EXTI Line
		// SYSCFG_EXTILineConfig(BUTTON_PORT_SOURCE[Button], BUTTON_PIN_SOURCE[Button]);

		EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[Button];
		EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
		EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
		EXTI_InitStructure.EXTI_LineCmd = ENABLE;
		EXTI_Init(&EXTI_InitStructure);

		NVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn[Button];
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);
	}
	else
	{
		/* code */
	}
	
}


uint32_t stm32_PBGetState(Button_TypeDef Button)
{
	return GPIO_ReadInputDataBit(BUTTON_PORT[Button], BUTTON_PIN[Button]);
}


static USART_TypeDef* COM_USART[COMn] = {EVAL_COM1}; 
static GPIO_TypeDef* COM_TX_PORT[COMn] = {EVAL_COM1_TX_GPIO_PORT};
static GPIO_TypeDef* COM_RX_PORT[COMn] = {EVAL_COM1_RX_GPIO_PORT};
static const uint32_t COM_USART_CLK[COMn] = {EVAL_COM1_CLK};
static const uint32_t COM_TX_PORT_CLK[COMn] = {EVAL_COM1_TX_GPIO_CLK};
static const uint32_t COM_RX_PORT_CLK[COMn] = {EVAL_COM1_RX_GPIO_CLK};
static const uint16_t COM_TX_PIN[COMn] = {EVAL_COM1_TX_PIN};
static const uint16_t COM_RX_PIN[COMn] = {EVAL_COM1_RX_PIN};

void stm32_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	// 使能GPIO时钟
	RCC_APB2PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM], ENABLE);
	
	// 使能USART时钟
	RCC_APB2PeriphClockCmd(COM_USART_CLK[COM], ENABLE);

	// GPIO_PinAFConfig(COM_TX_PORT[COM], COM_TX_PIN_SOURCE[COM], COM_TX_AF[COM]);
	// GPIO_PinAFConfig(COM_RX_PORT[COM], COM_RX_PIN_SOURCE[COM], COM_RX_AF[COM]);

	// 配置TX引脚
	GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM];
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure);
	
	// 配置RX引脚
	GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM];
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure);
	
	
	// 初始化USART
	USART_Init(COM_USART[COM], USART_InitStruct);
	
	// 使能USART
	USART_Cmd(COM_USART[COM], ENABLE);
}
